from django.db import models
from utils.BaseModels import BaseModel
from goods.models import Good
from users.models import UserProfile
from records.models import Record
from .manager import OrderManager, OrderDetailManager


# 订单
class Order(BaseModel.FullAtBaseModel):
    orderId = models.CharField('订单号', default='', max_length=128, unique=True)
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, db_index=True, verbose_name='会员')
    totalMoney = models.DecimalField('总原价', default=0.00, max_digits=12, decimal_places=2)
    totalDiscountMoney = models.DecimalField('总折后售价', default=0.00, max_digits=12, decimal_places=2)
    totalActualCharge = models.DecimalField('实际收取', default=0.00, max_digits=12, decimal_places=2)
    records = models.ManyToManyField(Record, default=None, null=True, blank=True, verbose_name='病症')
    note = models.TextField(verbose_name='备注', default='', null=True, blank=True)
    discount = models.DecimalField('折扣', default=0.00, max_digits=12, decimal_places=2)
    type = models.IntegerField('销售方式(0=赠送,1=预存款,2=积分)', default=1, choices=((1, '预存款'), (2, '积分'), (0, '赠送')))
    total_integral = models.IntegerField('总积分', default=0)
    state = models.IntegerField('状态', default=0, choices=((0, '正常'), (1, '废除')))

    objects = OrderManager()

    class Meta:
        db_table = 'orders'
        verbose_name = '订单'
        verbose_name_plural = '订单管理'


# 订单明细
class OrderDetail(BaseModel.CreateAndUpdateAtBaseModel):
    good = models.ForeignKey(Good, related_name='orderDetail', on_delete=models.CASCADE, verbose_name='商品')
    price = models.DecimalField('单价', default=0.00, max_digits=12, decimal_places=2)
    num = models.IntegerField('数量')
    totalMoney = models.DecimalField('总原价', default=0.00, max_digits=12, decimal_places=2)
    totalDiscountMoney = models.DecimalField('总折后售价', default=0.00, max_digits=12, decimal_places=2)
    totalActualCharge = models.DecimalField('实际收取', default=0.00, max_digits=12, decimal_places=2)
    order = models.ForeignKey(Order, related_name='orderDetail',  on_delete=models.CASCADE, verbose_name='订单')
    discount = models.DecimalField('折扣', default=0.00, max_digits=12, decimal_places=2)
    total_integral = models.IntegerField('总积分', default=0)

    objects = OrderDetailManager()

    class Meta:
        db_table = 'orderdetails'
        verbose_name = '订单明细'
        verbose_name_plural = '订单明细管理'